﻿<UserControl x:Class="CjcPhotoPrint.Preview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="clr-namespace:CjcPhotoPrint"
	Name="preview">

	<Grid>

		<Grid.Resources>

			<BooleanToVisibilityConverter x:Key="boolVisibilityConverter"/>

			<DataTemplate DataType="{x:Type local:IdentityContext}">

				<ContentControl Content="{Binding Transformation}"/>

			</DataTemplate>

			<DataTemplate DataType="{x:Type local:TransformationContext}">

				<ContentControl Content="{Binding Transformation}"/>

			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Identity}">

				<local:TransformedImage DataContext="{Binding ElementName=preview, Path=Source}"
										Drawing="{Binding Drawing}"/>

			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Rotate}">

				<local:TransformedImage DataContext="{Binding ElementName=preview, Path=Source}"
										Drawing="{Binding Drawing}"/>

			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Crop}">

				<Grid DataContext="{Binding ElementName=preview, Path=Source}">

					<local:TransformedImage Drawing="{Binding Parent.Drawing}"/>

					<Rectangle Fill="#80000000"
							   Visibility="{Binding Enabled, Converter={StaticResource boolVisibilityConverter}}"
							   MouseDown="Rectangle_MouseDown"
							   MouseMove="Rectangle_MouseMove"
							   MouseUp="Rectangle_MouseUp"
							   MouseLeave="Rectangle_MouseLeave"
							   Loaded="cropSelect_Loaded">
						<Rectangle.OpacityMask>
							<DrawingBrush>
								<DrawingBrush.Drawing>
									<DrawingGroup>
										<GeometryDrawing Brush="Transparent">
											<GeometryDrawing.Geometry>
												<RectangleGeometry Rect="0,0,1,1"/>
											</GeometryDrawing.Geometry>
										</GeometryDrawing>
										<GeometryDrawing Brush="White">
											<GeometryDrawing.Geometry>
												<CombinedGeometry GeometryCombineMode="Exclude">
													<CombinedGeometry.Geometry1>
														<RectangleGeometry Rect="0,0,1,1"/>
													</CombinedGeometry.Geometry1>
													<CombinedGeometry.Geometry2>
														<RectangleGeometry Rect="{Binding Transformation.Bounds}"/>
													</CombinedGeometry.Geometry2>
												</CombinedGeometry>
											</GeometryDrawing.Geometry>
										</GeometryDrawing>
									</DrawingGroup>
								</DrawingBrush.Drawing>
							</DrawingBrush>
						</Rectangle.OpacityMask>
					</Rectangle>

				</Grid>

			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Straighten}">

				<Grid DataContext="{Binding ElementName=preview, Path=Source}">

					<local:TransformedImage Drawing="{Binding Drawing}"/>

					<Grid ShowGridLines="{Binding Enabled}">
						<Grid.RowDefinitions>
							<RowDefinition/>
							<RowDefinition/>
							<RowDefinition/>
							<RowDefinition/>
							<RowDefinition/>
						</Grid.RowDefinitions>
						<Grid.ColumnDefinitions>
							<ColumnDefinition/>
							<ColumnDefinition/>
							<ColumnDefinition/>
							<ColumnDefinition/>
							<ColumnDefinition/>
						</Grid.ColumnDefinitions>
					</Grid>

					<Border CornerRadius="10" Background="#50FFFFFF" Padding="5" Margin="5"
							VerticalAlignment="Bottom" HorizontalAlignment="Stretch"
							Visibility="{Binding Enabled, Converter={StaticResource boolVisibilityConverter}}">

						<Slider Value="{Binding Path=Transformation.Angle, Mode=TwoWay}"
								Minimum="-30" Maximum="30"/>

					</Border>

				</Grid>

			</DataTemplate>

		</Grid.Resources>

		<ContentControl Content="{Binding ElementName=preview, Path=Source}"/>

    </Grid>

</UserControl>